﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Model;
using BLL;
using System.Configuration;

public partial class PersonInfo_MessageEdit : BasePage
{
    private Access access = new Access();
    promotionBO mbo = new promotionBO();
    private static int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            if (GetQeuryString("para") == "update")
            {
                string id = GetQeuryString("id");
                promotion m = mbo.GetModel(int.Parse(id));
                InitData(m);
            }
            else if (GetQeuryString("para") == "add")
            {
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        promotion m = GetEntity();
        if (GetQeuryString("para") == "add")
        {

            mbo.Add(m);
            AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");

        }
        else if (GetQeuryString("para") == "update")
        {
            mbo.Update(m);
            AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string FileName = file.FileName;
        string path = HttpContext.Current.Request.MapPath("Images/");

        if (!string.IsNullOrEmpty(FileName))
        {
            string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
            if ((extension.ToLower() == ".jpg"))
            {
                int fileSize = file.PostedFile.ContentLength;
                if (fileSize == 0)
                {
                   // base.Alertpromotion("文件不存在！");
                }
                //else if (fileSize > 52100)
                //{
                //    Alertpromotion("请上传50K以内的JPG格式照片");
                //}
                else
                {
                    //if (File.Exists(path + CurrentUserInfo.ID + ".jpg"))
                    //{
                    //    File.Delete(path + CurrentUserInfo.ID + ".jpg");
                    //}
                    if (true)//限制大小为1M
                    {
                        string guid = Guid.NewGuid().ToString();
                        file.SaveAs(path + guid + ".jpg");  //将文件上传到服务器
                        //if (File.Exists(path + guid + ".jpg"))
                        //{
                        //    Image1.ImageUrl = "Images/" + guid + ".jpg";
                        //    txtJJ.Text = ConfigurationManager.AppSettings["ServerURL"] + "/PersonInfo/Images/" + guid + ".jpg";
                        //}
                    }
                }
            }
            else
            {
               // Alertpromotion("请上传JPG格式图片!");
            }
        }
        else
        {
            //Alertpromotion("请选择文件.");
        }
    }

    #region 私有方法
    private promotion GetEntity()
    {
        promotion m = new promotion();
        if (GetQeuryString("para") == "update")
        {
            string id = GetQeuryString("id");
             m = mbo.GetModel(int.Parse(id));
        }
        //m.Content = content1.InnerText;
        m.Content = txtdz.Text;
        m.BeginDate = DateTime.Parse(txtTime.Text);
        m.Enddate = DateTime.Parse(txtTime2.Text);
        m.IF_ALL = 1;
        m.IF_MEIYU = 1;
        m.IF_REGIST = 1;
        m.IF_VIP = 1;
        m.Name = txtTitle.Text;
        m.PTYPE = ddlType.SelectedValue;
        m.STATUS = ddlStatus.SelectedValue;
        return m;
    }
    private void InitData(promotion m)
    {
        txtTime.Text = m.BeginDate.ToString();
        txtTime2.Text = m.Enddate.ToString();
        txtTitle.Text = m.Name;
        //content1.InnerText = m.Content;
        txtdz.Text = m.Content;
        ddlType.SelectedValue = m.PTYPE;
        ddlStatus.SelectedValue = m.STATUS;
       //Image1.ImageUrl = m.JJ;
       
    }

    #endregion
}